package TUILayer;
import ControlLayer.*;


public class LoanUI
{

    private LoanCtr loanCtr;
    private AddressBookCtr addressBookCtr;
    private DVDCtr dvdCtr;
    private InputParser inputParser;
    public LoanUI(InputParser inputParser)
    {
        loanCtr = new LoanCtr();
        addressBookCtr = new AddressBookCtr();
        dvdCtr = new DVDCtr();
        this.inputParser = inputParser;
    }

    public void start()
    {
        int choise = 0;
        boolean exit = false;
        while(choise < 1 || choise > 7 || !exit)
        {
            choise = inputParser.getInt("\f *** LoanMenu ***\n\n1. Show loan information\n2. Add loan\n3. Return loan\n4. Delete Loan\n5. Update loan\n6. List all loans\n7. Return\n\nMake your choise: ");
            if(choise == 1)
            {
                showInfo();
            }
            else if(choise == 2)
            {
                createLoan();
            }
            else if(choise == 3)
            {
                returnLoan();
            }
            else if(choise == 4)
            {
                deleteLoan();
            }
            else if(choise == 5)
            {
                updateLoan();
            }
            else if(choise == 6)
            {
                listAllLoans();
            }
            else if(choise == 7)
            {
                exit = true;
            }

        }
    }

    private void showInfo()
    {
        int id = inputParser.getInt("\f ***LoanInfoMenu ***\n\nPlease input the id of the loan: ");
        System.out.println("\f ***LoanInfoMenu ***\n");
        if(loanCtr.getLoan(id) != null)
        {
            loanCtr.printLoan(id);
        }
        else
        {
            System.out.println("Error: Loan not found");
        }
        inputParser.printReturn();
    }

    private void returnLoan()
    {
        int id = inputParser.getInt("\f ***ReturnLoanMenu ***\n\nPlease input the id of the loan: ");
        System.out.println("\f ***ReturnLoanMenu ***\n");
        if(loanCtr.getLoan(id) != null)
        {
            loanCtr.returnLoan(id, inputParser.getCurrentDate());
            System.out.println("Loan returned");
        }
        else
        {
            System.out.println("Error: Loan not found");
        }
        inputParser.printReturn();
    }

    private void createLoan()
    {
        int personID = inputParser.getInt("\f ***CreateLoanMenu ***\n\nPlease input the id of the person thah makes the loan: ");
        if(addressBookCtr.getPerson(personID) != null)
        {
            int dvdNr = inputParser.getInt("\f ***CreateLoanMenu ***\n\nPlease input the id of the DVD: ");
            if(dvdCtr.getDVD(dvdNr) != null)
            {
                int copySerialNr = inputParser.getInt("\f ***CreateLoanMenu ***\n\nPlease input the id of the copy: ");
                if(dvdCtr.getCopy(dvdNr, copySerialNr) != null)
                {
                    if(dvdCtr.getCopyStatus(dvdNr, copySerialNr).equals("Available"))
                    {
                        int loanID = inputParser.getInt("\f ***CreateLoanMenu ***\n\nPlease input the id of the new loan: ");
                        if(loanCtr.getLoan(loanID) == null)
                        {
                            long borrowDate = inputParser.getCurrentDate();
                            int borrowPeriod = inputParser.getInt("\f ***CreateLoanMenu ***\n\nPlease input the number of days the loan is made: ");
                            loanCtr.creatLoan(personID, dvdNr, copySerialNr, loanID, borrowDate, borrowPeriod);
                            System.out.println("\f ***CreateLoanMenu ***\n\nLoan added.");
                        }
                        else
                        {
                            System.out.println("\f ***CreateLoanMenu ***\n\nError: Id already exists in the loan collection");
                        }
                    }
                    else
                    {
                        System.out.println("\f ***CreateLoanMenu ***\n\nError: This copy is not available");
                    }
                }
                else
                {
                    System.out.println("\f ***CreateLoanMenu ***\n\nError: Copy not found");
                }
            }
            else
            {
                System.out.println("\f ***CreateLoanMenu ***\n\nError: DVD not found");
            }

        }
        else
        {
            System.out.println("\f ***CreateLoanMenu ***\n\nError: Person not found");
        }
        inputParser.printReturn();
    }

    private void deleteLoan()
    {
        int id = inputParser.getInt("\f ***DeleteLoanMenu ***\n\nPlease input the id of the loan: ");
        System.out.println("\f ***DeleteLoanMenu ***\n");
        if(loanCtr.getLoan(id) != null)
        {
            loanCtr.deleteLoan(id);
            System.out.println("Loan deleted.");
        }
        else
        {
            System.out.println("Error: Loan not found");
        }
        inputParser.printReturn();
    }

    private void listAllLoans()
    {
        System.out.println("\f ***ListAllLoanDMenu ***\n");
        loanCtr.printAllLoan();
        inputParser.printReturn();
    }

    private void updateLoan()
    {
        int id = inputParser.getInt("\f ***UpdateLoanMenu ***\n\nPlease input the id of the loan: ");
        if(loanCtr.getLoan(id) != null)
        {
            int choise = 0;
            String value = "";
            boolean exit = false;
            while(choise > 0 && choise < 5 || !exit)
            {
                choise = inputParser.getInt("\f ***UpdateLoanMenu ***\n\n1. Edit borrow date\n2. Edit period\n3. Edit state\n4. Return\n\nMake your choise: ");
                if(choise > 0 && choise < 5)
                {
                    if(choise == 4)
                    {
                        choise = 5;
                        exit = true;
                    }
                    else
                    {
                        if(choise == 1)
                        {
                            value = String.valueOf(inputParser.getDate("\f ***UpdateLoanMenu ***\n\nPlease input the new date (dd/mm/yyyy) of the loan: "));
                        }
                        else if(choise == 2)
                        {
                            value = String.valueOf(inputParser.getInt("\f ***UpdateLoanMenu ***\n\nPlease input the new period of the loan: "));
                        }
                        else if(choise == 3)
                        {
                            value = inputParser.getString("\f ***UpdateLoanMenu ***\n\nPlease input the new state of the loan: ");
                        }
                        loanCtr.updateLoan(choise, id, value);
                        System.out.println("\f ***UpdateLoanMenu ***\n\nLoan updated");
                        inputParser.printReturn();
                    }
                }
            }
        }
        else
        {
            System.out.println("\f ***UpdateLoanMenu ***\n\nError: Loan not found");
            inputParser.printReturn();
        }
    }

}
